import result.Result;
import util.Client;
import view.Login;
import view.Room;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;

/**
 * @author 化尘成影
 * @Description
 * @date 2022/6/27 13:36
 */
public class doMain {
    public static void main(String[] args) throws Exception{
        Client.connection();
        Client.serverStillLive();
        Thread.sleep(200);
        //判断是否能直接登录
        //读取token
        String rootPath = System.getProperty("user.dir");//获取项目路径
        Path path = Paths.get(rootPath + "\\Client\\src\\resource\\token.txt");
        String tokenValue = "";
        try {
            FileInputStream fileInputStream = new FileInputStream(path.toFile());
            int size = fileInputStream.available(); // size  为字串的长度 ，这里一次性读完
            byte[] bytes = new byte[size];
            fileInputStream.read(bytes);
            fileInputStream.close();
            tokenValue = new String(bytes);
        } catch (Exception exception){
            exception.printStackTrace();
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("token",tokenValue);
        Result result = Client.request("loginByToken", map, true);
        if(result.getCodeMsg().getCode() == 200){
            Room room = new Room(result.getMap().get("username"));
            room.setVisible(true);
        } else {
            Login login = new Login();
            login.setVisible(true);
        }
    }
}
